<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author=>冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace api\modules\admin\controllers\system;

use api\modules\admin\AuthController;
use api\utils\libs\CommonAction;
use common\cache\DictCache;
use common\models\system\DictData;
use common\models\system\DictType;

class DictTypeController extends AuthController
{
    use CommonAction;

    protected string $model = DictType::class;
    protected bool $validate = true;

    protected function saveAfter(DictType $model, string $type, array $oldData = []): void
    {
        if ($type == 'edit') {
            if (isset($model->type) && isset($oldData['type']) && $model->type != $oldData['type']){
                DictData::updateAll(['type'=>$model->type], ['type'=>$oldData['type']]);
            }
        }
    }

    public function actionCache(): array
    {
        DictCache::clear(true);
        return $this->success();
    }

    protected function exportBefore(array $list): array
    {
        $row = [
            'id' => '编码',
            'name' => '字典名称',
            'type' => '字典类型',
            'remark' => '备注',
            'update_time' => '更新时间'
        ];
        foreach ($list as $key => $value) {
            $list[$key]['update_time'] = date('Y-m-d H:i:s', $value['update_time']);
        }
        $bold = ['A1:E1' => true];
        $width = ['a' => 10, 'b' => 20, 'c' => 20, 'd' => 40, 'e' => 15];
        $size = ['a1:A6' => 18];
        $wrap = ['d' => true];
        $background = ['b' => 'FF0000'];
        $color = ['a1' => 'FF0000'];
        return ['list' => $list, 'row' => $row, 'fileName' => '字典类型列表', 'bold' => $bold, 'width' => $width, 'size' => $size, 'wrap' => $wrap, 'background' => $background, 'color' => $color];
    }
}
